<?php
class ConfigurationController extends Core_Controller
{
    public function indexAction()
    {
        if(count($this->_http->getArvs())>0) throw new Exception("404 not found",404);
        if(! isset($_SESSION['admin_id'])) $this->redirect('/admin');
        
        $this->_view->title = "Cấu hình hệ thống";
        $this->_view->activeClass = "configuration";
        
        $fileConfig = APPLICATION_PATH."/configs/config.php";
        
        $registry = Core_Registry::singleton();
        $config       = $registry['config'];
        
        //0: chua submit(van co quyen ghi file)  1: khong co quyen ghi  2: submit thanh cong
        $status = 0;
        $message = '';
        
        if(! is_writable($fileConfig))
        {
        	$status = 1;
        	$message = "File config.php không có quyền ghi.";	
        }
       
       if(($status != 1)&&(isset($_POST['error'])))
       {
       	 $content = file_get_contents($fileConfig);
       	 if($content)
       	 {
       	 	$content = "\n".$content;
       	 	
       	 	$error = ($_POST['error'] == '1')?'true':'false';
       	 	$config['errorReporting'] = ($_POST['error'] == '1')?true:false;
       	 	$content = preg_replace("/(\n\\$)errorReporting(.*)[^\n]/", "\n$"."errorReporting = $error;",$content);
       	 	
       	 	$error404 = ($_POST['error404'] == '1')?'true':'false';
       	 	$config['error404']['status'] = ($_POST['error404'] == '1')?true:false;
       	 	$content = preg_replace("/(\n\\$)config\[\'error404\'\]\[\'status\'\](.*)[^\n]/", "\n$"."config['error404']['status'] = $error404;",$content);
       	 	
       	 	$https = ($_POST['https'] == '1')?'true':'false';
       	 	$config['https'] = ($_POST['https'] == '1')?true:false;
       	 	$content = preg_replace("/(\n\\$)config\[\'https\'\](.*)[^\n]/", "\n$"."config['https'] = $https;",$content);
       	 	
       	 	$emailAdmin = $_POST['emailAdmin'];
       	 	$config['email']['email_admin'] = $emailAdmin;
       	 	$content = preg_replace("/(\n\\$)config\[\'email\'\]\[\'email_admin\'\](.*)[^\n]/", "\n$"."config['email']['email_admin'] = '$emailAdmin';",$content);
       	 	
       	 	$emailContact = ($_POST['emailContact'] == '1')?'true':'false';
       	 	$config['email']['receiver_contact'] = ($_POST['emailContact'] == '1')?true:false;
       	 	$content = preg_replace("/(\n\\$)config\[\'email\'\]\[\'receiver_contact\'\](.*)[^\n]/", "\n$"."config['email']['receiver_contact'] = $emailContact;",$content);
       	 	
       	 	$emailRegister = ($_POST['emailRegister'] == '1')?'true':'false';
       	 	$config['email']['send_register'] = ($_POST['emailRegister'] == '1')?true:false;
       	 	$content = preg_replace("/(\n\\$)config\[\'email\'\]\[\'send_register\'\](.*)[^\n]/", "\n$"."config['email']['send_register'] = $emailRegister;",$content);
       	 	
       	 	$contentEmail = $_POST['contentEmail'];
       	 	$order   = array("\r\n", "\n", "\r");
       	 	$contentEmail = str_replace($order, " ", $contentEmail);
       	 	$config['email']['send_register_content'] = $contentEmail;
       	 	$content = preg_replace("/(\n\\$)config\[\'email\'\]\[\'send_register_content\'\](.*)[^\n]/", "\n$"."config['email']['send_register_content'] = '$contentEmail';",$content);
       	 	
       	 	$isSMTP = ($_POST['isSMTP'] == '1')?'true':'false';
       	 	$config['smtp']['is_smtp'] = ($_POST['isSMTP'] == '1')?true:false;
       	 	$content = preg_replace("/(\n\\$)config\[\'smtp\'\]\[\'is_smtp\'\](.*)[^\n]/", "\n$"."config['smtp']['is_smtp'] = $isSMTP;",$content);
       	 	
       	 	$SMTPAuth = ($_POST['SMTPAuth'] == '1')?'true':'false';
       	 	$config['smtp']['smtp_auth'] = ($_POST['SMTPAuth'] == '1')?true:false;
       	 	$content = preg_replace("/(\n\\$)config\[\'smtp\'\]\[\'smtp_auth\'\](.*)[^\n]/", "\n$"."config['smtp']['smtp_auth'] = $SMTPAuth;",$content);
       	 	
       	 	$SMTPSecure = $_POST['SMTPSecure'];
       	 	$config['smtp']['smtp_secure'] = $SMTPSecure;
       	 	$content = preg_replace("/(\n\\$)config\[\'smtp\'\]\[\'smtp_secure\'\](.*)[^\n]/", "\n$"."config['smtp']['smtp_secure'] = '$SMTPSecure';",$content);
       	 	
       	 	$SMTPHost = $_POST['SMTPHost'];
       	 	$config['smtp']['host'] = $SMTPHost;
       	 	$content = preg_replace("/(\n\\$)config\[\'smtp\'\]\[\'host\'\](.*)[^\n]/", "\n$"."config['smtp']['host'] = '$SMTPHost';",$content);
       	 	
       	 	$SMTPPort = intval($_POST['SMTPPort']);
       	 	$config['smtp']['port'] = $SMTPPort;
       	 	$content = preg_replace("/(\n\\$)config\[\'smtp\'\]\[\'port\'\](.*)[^\n]/", "\n$"."config['smtp']['port'] = $SMTPPort;",$content);
       	 	
       	 	$SMTPUsername = $_POST['SMTPUsername'];
       	 	$config['smtp']['username'] = $SMTPUsername;
       	 	$content = preg_replace("/(\n\\$)config\[\'smtp\'\]\[\'username\'\](.*)[^\n]/", "\n$"."config['smtp']['username'] = '$SMTPUsername';",$content);
       	 	
       	 	$SMTPPassword = $_POST['SMTPPassword'];
       	 	$config['smtp']['password'] = $SMTPPassword;
       	 	$content = preg_replace("/(\n\\$)config\[\'smtp\'\]\[\'password\'\](.*)[^\n]/", "\n$"."config['smtp']['password'] = '$SMTPPassword';",$content);
       	 	
       	 	
       	 	$content = trim($content);
       	 	file_put_contents($fileConfig,$content);
       	 	$status = 2;
       	 }
       } 
       $this->_view->status = $status;
       $this->_view->message = $message;
       $this->_view->config = $config;  	
    }
}